<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Gets an SplFileObject object for the file</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="splfileinfo.iswritable.html">« SplFileInfo::isWritable</a></li>
      <li style="float: right;"><a href="splfileinfo.setfileclass.html">SplFileInfo::setFileClass »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.splfileinfo.html">SplFileInfo</a></li>
    <li>Gets an SplFileObject object for the file</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="splfileinfo.openfile" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">SplFileInfo::openFile</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.2, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">SplFileInfo::openFile</span> &mdash; <span class="dc-title">Gets an SplFileObject object for the file</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-splfileinfo.openfile-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>SplFileInfo::openFile</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$mode</code><span class="initializer"> = &quot;r&quot;</span></span>, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$useIncludePath</code><span class="initializer"> = <strong><code>false</code></strong></span></span>, <span class="methodparam"><span class="type">?</span><span class="type"><span class="type">resource</span><span class="type"></span></span> <code class="parameter">$context</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><a href="class.splfileobject.html" class="type SplFileObject">SplFileObject</a></span></div>

  <p class="para rdfs-comment">
   Creates an <span class="classname"><a href="class.splfileobject.html" class="classname">SplFileObject</a></span> <span class="type">object</span> of
   the file. This is useful because <span class="classname"><a href="class.splfileobject.html" class="classname">SplFileObject</a></span>
   contains additional methods for manipulating the file whereas
   <span class="classname"><a href="class.splfileinfo.html" class="classname">SplFileInfo</a></span> is only useful for gaining information,
   like whether the file is writable.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-splfileinfo.openfile-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">mode</code></dt>

     <dd>

      <p class="para">
       The mode for opening the file. See the <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span>
       documentation for descriptions of possible modes. The default 
       is read only.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">useIncludePath</code></dt>

     <dd>

      <p class="para">
       值 设为 <strong><code>true</code></strong> 时, 也会在
<a href="ini.core.html#ini.include-path" class="link">include_path</a>搜索文件名。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">context</code></dt>

     <dd>

      <p class="para">
       <code class="literal">上下文（context）</code>的说明请参考手册中的 <a href="context.html" class="link">上下文（context）</a>章节.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-splfileinfo.openfile-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   The opened file as an <span class="classname"><a href="class.splfileobject.html" class="classname">SplFileObject</a></span> <span class="type">object</span>.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-splfileinfo.openfile-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   A <span class="classname"><a href="class.runtimeexception.html" class="classname">RuntimeException</a></span> if the file cannot be
   opened (e.g. insufficient access rights).
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-splfileinfo.openfile-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.0.0</td>
       <td>
        <code class="parameter">context</code> is now nullable.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-splfileinfo.openfile-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3694">
    <p><strong>示例 #1 <span class="methodname"><strong>SplFileInfo::openFile()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$fileinfo&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SplFileInfo</span><span style="color: #007700">(</span><span style="color: #DD0000">'/tmp/foo.txt'</span><span style="color: #007700">);<br /><br />if&nbsp;(</span><span style="color: #0000BB">$fileinfo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">isWritable</span><span style="color: #007700">())&nbsp;{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$fileobj&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$fileinfo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">openFile</span><span style="color: #007700">(</span><span style="color: #DD0000">'a'</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$fileobj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #DD0000">"appended&nbsp;this&nbsp;sample&nbsp;text"</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-splfileinfo.openfile-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="classname"><a href="class.splfileobject.html" class="classname">SplFileObject</a></span></li>
    <li class="member"><span class="function"><a href="function.stream-context-create.html" class="function" rel="rdfs-seeAlso">stream_context_create()</a> - 创建资源流上下文</span></li>
    <li class="member"><span class="function"><a href="function.fopen.html" class="function" rel="rdfs-seeAlso">fopen()</a> - 打开文件或者 URL</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>